package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.ALiYunUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;

/**
 * @description: 套餐的业务层
 * @author: wangjingliang
 * @create: 2019-11-09 21:22
 **/
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;
    @Autowired
   private RedisTemplate redisTemplate;

    /**
     * 根据条件进行分页查询
     *
     * @param: QueryPageBean
     * @return: null
     * @date: 2019/11/6
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = null;
        try {
            pageResult = setmealService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, MessageConstant.EXCEPION);
        }
    }


    /**
     * url:"/setmeal/upload.do"
     * @param:
     * @return: null
     * @date: 2019/11/11
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        //原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        //获取后缀名.jpg
        String extention = originalFilename.substring(index);
        //文件名+后缀
        String fileName = UUID.randomUUID().toString().replace("-", "") + extention;

        try {
            ALiYunUtils.update(fileName,imgFile);
            //储存到redis中
            redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_RESOURCES).add(fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL, MessageConstant.EXCEPION);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
    }

    /**
     * url:/setmeal/add.do?checkgroupIds=" + this.checkgroupIds,this.formData
     *
     * @param:
     * @return: null
     * @date: 2019/11/12
     */
    @PostMapping("/add")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        int add;
        try {
            add = setmealService.add(checkgroupIds, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL, e.getMessage());
        }
        //判断是否有返回值,是多少
        if (add == 0) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL, MessageConstant.EXCEPION);
        }
        //存入redis中
        redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_DB_RESOURCES).add(setmeal.getImg());
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }


}
